package com.xintoucloud.pbs.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xintoucloud.pbs.dao.OrganizationDao_1;
import com.xintoucloud.pbs.dao.PartyMemberDao;
import com.xintoucloud.pbs.entity.Organization;
import com.xintoucloud.pbs.entity.OrganizationStage;
import com.xintoucloud.pbs.entity.OrganizationState;
import com.xintoucloud.pbs.entity.PartyMember;
import com.xintoucloud.pbs.model.CheckArr;
import com.xintoucloud.pbs.model.OrganizationInformationDto;
import com.xintoucloud.pbs.model.OrganizationNode;
import com.xintoucloud.pbs.model.Status;
import com.xintoucloud.pbs.model.TreeData;
import com.xintoucloud.pbs.model.TreeNode;
import com.xintoucloud.pbs.service.OrganizationService;
@Service@Transactional
public class OrganizationServiceImpl implements OrganizationService{
	@Autowired
	private OrganizationDao_1 organizationDao;
	@Autowired
	private PartyMemberDao partyMemberDao;
	
	@Override
	public List<OrganizationNode> listOrganizationByParentId(Integer parentId) {
		List<Organization> Organizations=null;
		if(parentId==0) {
			//查找所有父节点为0的
			Organizations=organizationDao.listOrganizationsIsZeroParentId();
		}else {
			Organizations=organizationDao.listOrganizationsByParentId(parentId);
		}
		List<OrganizationNode> organizationNodes=new ArrayList<>();
		for (Organization organization : Organizations) {
			OrganizationNode organizationNode=new OrganizationNode();
			organizationNode.setName(organization.getOname());
			organizationNode.setId(organization.getOid());
			organizationNode.setEalias(organization.getBname());
			List<OrganizationNode> childs=listOrganizationByParentId(organization.getOid());
			organizationNode.setChildren(childs);
			if(childs!=null) {
			organizationNodes.add(organizationNode);
				organizationNode.setSpread(true);
			}else {
				organizationNode.setSpread(false);
			}
		}
		return organizationNodes;
}


	@Override
	@Transactional(readOnly=true)
	public Organization getOne(Integer oid) {
		return organizationDao.getOne(oid);
	}

	@Override
	public TreeData getTreeData(Integer parentId) {
		TreeData treeData = new TreeData();
		Status status=new Status();
		List<TreeNode> treeNodes=listTreeNode(parentId);
		if(treeNodes!=null) {
			status.setCode(200);
			status.setMessage("加载成功");
		}
			treeData.setData(treeNodes);
			treeData.setStatus(status);
		return treeData;
	}

	@Override
	public List<TreeNode> listTreeNode(Integer parentId) {
		List<Organization> organizations=null;
		if(parentId==0) {
			//查找所有父节点为0的
			organizations=organizationDao.listOrganizationsIsZeroParentId();
		}else {
			organizations=organizationDao.listOrganizationsByParentId(parentId);
		}
		List<TreeNode> treeNodes=new ArrayList<>();
		for (Organization organization : organizations) {
			TreeNode treeNode=new TreeNode();
			treeNode.setId(organization.getOid());
			treeNode.setTitle(organization.getOname());
			treeNode.setLevel(organization.getOsg().getOsgid());
			treeNode.setParentId(organization.getParentid());
			List<CheckArr> checkArrs=new ArrayList<>();
			CheckArr checkArr=new CheckArr();
			checkArr.setIsChecked("0");
			checkArr.setType("0");
			checkArrs.add(checkArr);
			treeNode.setCheckArr(checkArrs);
			List<TreeNode> childs=listTreeNode(organization.getOid());
			treeNode.setChildren(childs);
			if(childs.isEmpty()) {
				treeNode.setisLast(true);
			}else {
				treeNode.setisLast(false);
			}
			treeNodes.add(treeNode);
		}
		return treeNodes;
	}

	@Override
	public boolean deleteById(int oid) {
		// 先查询其所有子节点
		boolean result=false;
		List<Organization> organizations=organizationDao.listOrganizationsByParentId(oid);
		//如果没有子节点。执行删除操作
		if(organizations.isEmpty()) {
			organizationDao.deleteOrganizationById(oid);
			return organizations.isEmpty();
		}else {
		//如果有子节点,遍历子节点集合，利用递归查寻有没有下一级的节点，如果有则继续遍历
		for (Organization organization : organizations) {
			result=deleteById(organization.getOid());
			}
			organizationDao.deleteOrganizationById(oid);
		}
		return result;
	}

	@Override
	public Organization saveOrganization(int parentId,String oName) {
		Organization org=new Organization();
		System.out.println("parentId:"+parentId);
		//org.setOid(oid);
		org.setParentid(parentId);
		org.setOname(oName);
		Organization org_parent=organizationDao.getOne(parentId);
		System.out.println("org_parent:"+org_parent.getOsg().getOsgid());
		OrganizationStage osg=new OrganizationStage();
		osg.setOsgid(org_parent.getOsg().getOsgid()+1);
		OrganizationState ost=new OrganizationState();
		ost.setOstid(org_parent.getOst().getOstid());
		org.setOsg(osg);
		org.setOst(ost);
		organizationDao.saveOrganization(org);
		return org;
	}


	@Override
	public TreeNode updateOrganization(int oid,String oName) {
		organizationDao.updateOrganization(oid,oName);;
		Organization org=organizationDao.getOne(oid);
		TreeNode treeNode=new TreeNode();
		treeNode.setId(org.getOid());
		treeNode.setisLast(false);
		treeNode.setLevel(org.getOsg().getOsgid());
		treeNode.setParentId(org.getParentid());
		treeNode.setCheckArr(null);
		treeNode.setTitle(org.getOname());
		List<TreeNode> childs=listTreeNode(org.getOid());
		treeNode.setChildren(childs);
		if(childs.isEmpty()) {
			treeNode.setisLast(true);
		}else {
			treeNode.setisLast(false);
		}
		return treeNode;
	}


	@Override
	public OrganizationInformationDto showOrganizationInformation(int oid) {
		OrganizationInformationDto oind=new OrganizationInformationDto();
		Organization org=organizationDao.getOne(oid);
		oind.setOid(org.getOid());
		oind.setOname(org.getOname());
		oind.setBname(org.getBname());
		oind.setLinkman(org.getLinkman());
		oind.setOphone(org.getOphone());
		oind.setPartysList(org.getMembersList());
		oind.setAddress(org.getAddress());
		oind.setCoordinate(org.getCoordinate());
		return oind;
	}


	@Override
	public void updateOrganization(Organization org) {
		organizationDao.updateOrganization(org);
	}


	@Override
	public List<String> showOrganizationMap() {
		List<String> list=organizationDao.listOrganizationMap();
		return list;
	}

}
